package com.framework.admin.controller;

import com.framework.admin.domain.dto.PassWordEmailDto;
import com.framework.admin.domain.param.SysUserParam;
import com.framework.admin.model.SysUser;
import com.framework.admin.service.IUserService;
import com.framework.admin.vo.SysUserVO;
import com.framework.common.domain.ParamPage;
import com.framework.common.domain.ResultData;
import com.framework.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理")
public class UserController {
    @Autowired
    private IUserService userService;

    @Log("用户新增")
    @ApiOperation("获取用户接口")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> userList(SysUser sysUser, ParamPage page) {
        PageRequest request = PageRequest.of(page.getPageNo()-1, page.getPageSize());
        Page<SysUserVO> users = userService.getUserList(sysUser, request);
        return ResultData.data(users);
    }

    @ResponseBody
    @ApiOperation(value = "获取用户列表")
    @PostMapping(value = "/all")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> allList(SysUser sysUser) {
        List<SysUser> list = userService.allList(sysUser);
        return ResultData.data(list);
    }

    @ApiOperation("获取单个用户接口")
    @GetMapping(value = "/one/{id}")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> getUserById(@PathVariable("id") String id) {
        SysUser user = userService.getUserById(id);
        return ResultData.data(user);
    }

    /**
     * 保存用户角色
     */
    @ResponseBody
    @ApiOperation(value = "保存用户角色")
    @PostMapping(value = "/save")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> save(@RequestBody SysUserParam user) {
        userService.save(user);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "启用禁用")
    @PostMapping(value = "/enable")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> enable(@RequestBody SysUser user) {
        userService.enable(user);
        return ResultData.success();
    }

    /**
     * 保存用户基础信息
     */
    @ResponseBody
    @ApiOperation(value = "保存用户")
    @PostMapping(value = "/info/save")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> saveInfo(@RequestBody SysUser user) {
        userService.saveInfo(user);
        return ResultData.success();
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @ApiOperation(value = "删除用户")
    @PostMapping(value = "/del")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<?> del(@RequestBody List<String> ids) {
        userService.delete(ids);
        return ResultData.success();
    }

    /**
     * 重置密码
     */
    @ResponseBody
    @ApiOperation(value = "重置密码")
    @PostMapping(value = "/resetPassword/{id}")
    public ResultData<?> resetPassword(@PathVariable String id) {
        userService.resetPassword(id);
        return ResultData.success();
    }

    /**
     * 修改密码
     */
    @ResponseBody
    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/changePassword/{id}")
    public ResultData<?> changePassword(@PathVariable String id, @RequestBody PassWordEmailDto dto) {
        userService.changePassword(id, dto);
        return ResultData.success();
    }

}
